import React, {Component} from 'react'
import {ListBox} from '../../../components'
import {fetchData} from '../../../components/AjaxConsumer'
import { Form, Modal, Button } from 'antd';
// 入库过渡库位选择弹出框
export default class BarcodeListModelDetail extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            k:'',
            rowObj: {},
            wdCode:'',
            url:'',
            userDefined4:''
        }
        this.selectRowObj = {};
        this.rowOperation = [
            {
                handleName: 'rowClick',
                showName: '行单击选择事件'
            }
        ];
        this.headerOperation = [];
       //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            data: [
                {
                    showName: '条码',
                    columnName: 'barcode'
                },{
                    showName: '型号',
                    columnName: 'model'
                },{
                    showName: '重量',
                    columnName: 'weight'
                },{
                    showName: '库区',
                    columnName: 'waCode'
                // },{
                //     showName: '库位',
                //     columnName: 'wlCode'
                },{
                    showName: '报废原因',
                    columnName: 'userDefined1'
                },{
                    showName: '是否登记',
                    columnName: 'userDefined4',
                    columnHidden:'true',
                    showOption:[
                        {
                            optionKey: '',
                            optionShow: ''
                        },{
                            optionKey: '0',
                            optionShow: '否'
                        },{
                            optionKey: '1',
                            optionShow: '是'
                        }
                    ]
                },{
                    showName: '操作人',
                    columnName: 'createBy'
                }
            ]
        };
        this.searchComponentData = [
            {
                showName: '库区',
                name: 'waCode',
                type: 'select',
                optionList: []
                // url: '/base/data/area/get/wa_code'
            // }, {
            //     showName: '库位',
            //     name: 'wlCode',
            //     type: 'select',
            //     // optionList: []
            //     url: '/base/data/location/get/wl_code'
            }, {
                showName: '报废原因',
                name: 'userDefined1',
                type: 'select',
                optionList: []
            }, {
                showName: '是否登记',
                name: 'userDefined4',
                type: 'select',
                optionList: [
                    {
                        optionKey: '',
                        optionShow: ''
                    },{
                        optionKey: '1',
                        optionShow: '是'
                    },{
                        optionKey: '0',
                        optionShow: '否'
                    }
                ]
            }
        ];
    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles == 'rowClick'){
            this.rowClickHandle(rowObj);
        }
    }

    rowClickHandle = (rowObj) => {    
        console.log('点击选中====',rowObj);    
        this.selectRowObj = rowObj[0].data;        
    }

    refreshHandle = () => {
        this.refs.listComponent.refreshHandle();
    }

    showModal = (k) => {
        this.setState({
            visible: true,
            k:k,
            // url:'/base/promodel/getModels/list'
            url:'/product/proscrapoutsn/list?userDefined4=0',
        });
        this.selectRowObj = {};
    }

    handleOk = () => {
        this.setState({
            visible: false
        })
        let resultData = {};
        resultData.k = this.state.k;

        resultData.barcode = this.selectRowObj.barcode;
        resultData.model = this.selectRowObj.model;
        resultData.weight = this.selectRowObj.weight;
        resultData.waCode = this.selectRowObj.waCode;
        // resultData.wlCode = this.selectRowObj.wlCode;
        resultData.userDefined1 = this.selectRowObj.userDefined1;
        resultData.createBy = this.selectRowObj.createBy;
        
        this.props.getTableData(resultData);
    }

    handleCancel = () => {
        this.setState({
            visible: false
        })
    }

    componentWillMount() {
        const dicType = 'scrap_reason';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);

        const optionsArea = {
            url: '/base/data/area/get/wa_code',
            type: 'get',
            condition:{}
        };
        fetchData(this, optionsArea, this.areaInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[4].showOption = optionList;
        this.searchComponentData[1].optionList = optionList;
    }
    areaInitCallBackHandle = (resData)=>{
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        this.tableColumn.data[3].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <Modal
                    style={{top: '20px',maxHeight:'500px'}}
                    visible={this.state.visible}
                    title='报废条码详情'
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'1015px'}
                    footer={[
                        <Button key="back" onClick={this.handleCancel}>取消</Button>,
                        <Button key="submit" type="primary" onClick={this.handleOk}>
                        确定
                        </Button>
                    ]}
                    maskClosable={false}
            >
                <ListBox
                    url = {this.state.url}
                    tableColumnData = {this.tableColumn}
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    ref = "listComponent"
                    searchComponentData={this.searchComponentData}
                    headerOperation = {this.headerOperation}
                    subtractHeight = '70'
                    subtractWidth ='360'
                    isRowSingle = "true"
                />
            </Modal>
        )
    }
}
